package com.mooer.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mooer.R;
import com.mooer.utils.UtilTools;
import com.mooer.utils.ui.UiUtils;

public class Home extends Activity implements OnClickListener {

	private GridView gridView;

	private HomeAdapter adapter;

	private ImageView searchButton;

	private EditText edit;

	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		gridView = (GridView) findViewById(R.id.gridview);
		searchButton = (ImageView) findViewById(R.id.search_btn);
		edit = (EditText) findViewById(R.id.search);
		adapter = new HomeAdapter();
		gridView.setAdapter(adapter);
		initGridView();
		searchButton.setOnClickListener(this);
	}

	private void initGridView() {
		Resources resources = getResources();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("icon", resources.getDrawable(R.drawable.b_near_selector));
		map1.put("name", "附近");
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("icon", resources.getDrawable(R.drawable.b_search_selector));
		map2.put("name", "搜索");
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("icon", resources.getDrawable(R.drawable.b_signin_selector));
		map3.put("name", "签到");
		Map<String, Object> map4 = new HashMap<String, Object>();
		map4.put("icon", resources.getDrawable(R.drawable.b_dynamic_selector));
		map4.put("name", "商户动态");
		Map<String, Object> map5 = new HashMap<String, Object>();
		map5.put("icon", resources.getDrawable(R.drawable.b_activity_selector));
		map5.put("name", "猫猫活动");
		Map<String, Object> map6 = new HashMap<String, Object>();
		map6.put("icon", resources.getDrawable(R.drawable.b_concern_selector));
		map6.put("name", "我的关注");
		Map<String, Object> map7 = new HashMap<String, Object>();
		map7.put("icon", resources.getDrawable(R.drawable.b_scan_selector));
		map7.put("name", "最近浏览");
		Map<String, Object> map8 = new HashMap<String, Object>();
		map8.put("icon", resources.getDrawable(R.drawable.b_more_selector));
		map8.put("name", "更多");
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		list.add(map6);
		list.add(map7);
		list.add(map8);
	}

	public class HomeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			LinearLayout _gridView = (LinearLayout) LayoutInflater.from(
					Home.this).inflate(R.layout.gridviewer_item, null);
			ImageView pic = (ImageView) _gridView.findViewById(R.id.pic);
			TextView text = (TextView) _gridView.findViewById(R.id.text);
			pic.setBackgroundDrawable((Drawable) list.get(position).get("icon"));
			text.setText((String) list.get(position).get("name"));
			pic.setTag(position);
			pic.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int p = (Integer) v.getTag();
					Intent intent = new Intent();
					if (p == 0) {
						intent.setClass(Home.this, Near.class);
						startActivity(intent);
					} else if (p == 1) {
						intent.setClass(Home.this, Search.class);
						startActivity(intent);
					} else if (p == 2) {
						intent.setClass(Home.this, Sign.class);
						startActivity(intent);
					} else if (p == 3) {
						intent.setClass(Home.this, BusinessDynamic.class);
						startActivity(intent);
					} else if (p == 4) {
						intent.setClass(Home.this, Activities.class);
						startActivity(intent);
					} else if (p == 5) {
						// 我的关注
						intent.setClass(Home.this, MyFollow.class);
						startActivity(intent);
					} else if (p == 6) {
						// 最近浏览
						intent.setClass(Home.this, RecentlyView.class);
						startActivity(intent);
					} else if (p == 7) {
						// 更多
						intent.setClass(Home.this, More.class);
						startActivity(intent);
					}
				}
			});
			return _gridView;
		}

	}

	@Override
	public void onClick(View v) {
		if (v == searchButton) {
			if (edit.getText().toString().length() > 0) {
				Intent intent = new Intent(this, BusinessList.class);
				intent.putExtra("type1", "搜索");
				intent.putExtra("type2", edit.getText().toString());
				intent.putExtra("content",
						UtilTools.encodeURL(edit.getText().toString()));
				startActivity(intent);
			} else {
				UiUtils.showToast(this, "请输入您要查询的关键字");
			}
		}

	}

}
